package code.c11;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/11/17 11:49
 * @package code.c11
 * @description 文件说明
 */

public class Test {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("加法计算器");

        // 设置组件
        TextField num1 = new TextField(5);
        TextField num2 = new TextField(5);
        TextField num3 = new TextField(5);
        JButton jButton = new JButton("计算");
        JLabel f1 = new JLabel("+");
        JLabel f2 = new JLabel("=");

        // 设置按钮监听器
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    // 取数字1 和数字2
                    Double num_1 = Double.parseDouble(num1.getText());
                    Double num_2 = Double.parseDouble(num2.getText());
                    // 输出结果
                    num3.setText(String.valueOf(num_1 + num_2));
                } catch (NumberFormatException numberFormatException) {
                    numberFormatException.printStackTrace();
                    System.out.println("输入有误，请输入正确的数字");
                }
            }
        });

        // 添加组件，并设置jframe 属性
        jFrame.add(num1);
        jFrame.add(f1);
        jFrame.add(num2);

        jFrame.add(f2);

        jFrame.add(num3);
        jFrame.add(jButton);

        jFrame.setSize(632,200);
        jFrame.setLayout(new FlowLayout());
        jFrame.setVisible(true);
    }
}
